Skip to content

DEV-1449: read the battery at its packet index, and turn it on from the inquiry - #30

Open
marknolan wants to merge 1 commit into
mainfrom
DEV-1449_battery_packet_index
Open

marknolan wants to merge 1 commit into
mainfrom
DEV-1449_battery_packet_index

Conversation

@marknolan

Copy link
Copy Markdown
Member

DEV-1449. The battery (VBATT, channel 0x03) is now read where it actually sits in the packet, and the inquiry response turns it on. That works on both a Shimmer3 and a Shimmer3R. This is older than the sound level work in #29, and was noted there under "Seen in passing".

What was wrong

  • Fixed offset. BattVoltageSensor.processData always read sensorPacket[3..<5], the first channel after the 3-byte timestamp. VBATT is only there when nothing comes before it:

    • on a Shimmer3, it follows the low-noise accel (0x00-0x02) whenever that is on;
    • on a Shimmer3R, it is the last channel.

    In the packets used by the new tests, bytes 3-4 are the magnetometer's, raw 46 or 43. That would have been reported as 63-67 mV.

  • Never on after connect. interpretDataPacketFormat starts with disableSensors(), and its VBatt case only added 2 to the packet size. So every inquiry turned the battery off and nothing turned it back on. connect(), writeInfoMem and setSensors all end with an inquiry, which undid the Shimmer3's InfoMem enable. The battery signal never reached the ObjectCluster on either generation.

  • Not set up on a Shimmer3R. The Shimmer3R branches of createSensors, initializeSensors and disableSensors skipped battVoltageSensor.

Changes

  • BattVoltageSensor has a packetIndex and reads the battery there. With no index yet, or one past the end of the packet, it adds nothing, rather than reading the wrong bytes or out of range.
  • The VBatt case of interpretDataPacketFormat sets the index and enables the sensor, as the other sensors' cases do.
  • The Shimmer3R branches create, configure and disable the battery sensor, as the Shimmer3 branches do. The InfoMem bit (sensors byte 1, bit 5) is the same on both.
  • The calibration moves into BattVoltageSensor.calibrate(raw:), unchanged.

Calibration: checked, no change

Swift's raw * 3000/4095 * 2 is what the other APIs use, on both generations:

  • Java driver: SensorADC.calibrateU12AdcValueToMillivolts(raw, 0, 3, 1) * 2, with no Shimmer3R branch.
  • C# API: a Shimmer3R branch that uses the same CalibrateU12AdcValue(raw, 0, 3, 1) * 2.0 as the Shimmer3 one.

Tests

Four cases are added to SensorTest:

  • testBatteryCalibration
  • testBatteryDecodesAtItsIndex
  • testBatteryWithoutAUsableIndexAddsNothing
  • testBatteryFromShimmer3RPackets

The last one uses four packets a Shimmer3R streamed, the same packets as the sound level test on #29:

  • 1024 Hz with every sensor on: the battery at byte 49, raw 2867 and 2872.
  • 512 Hz IMU only: the battery at byte 23, raw 2847 and 2861.

These cases need packetIndex, so they don't compile on main.

No Swift toolchain was available, so CI on this PR is the first time the code is compiled and run. The Shimmer3Protocol wiring (the VBatt case and the three Shimmer3R branches) isn't reachable from a unit test, because the protocol class needs a live CBPeripheral. It has not been run against a device.

Sibling APIs

None needs this fix. Each one finds the battery by its position in the inquiry's channel list, not at a fixed byte:

  • Java driver: getSignalIndex(BATTERY).
  • Android API: decodes through the Java driver.
  • C# API: getSignalIndex(V_SENSE_BATT), and the same in its Xamarin copy.
  • pyshimmer: builds its layout from the channel list, with VBATT as a 2-byte channel on both revisions.
  • TypeScript SDK: builds its stream schema offsets from the channel list.

With #29

Both PRs change Shimmer3Protocol.swift and SensorTest.swift, at different places. git merge-tree merges this branch with DEV-1413_sound_level cleanly.

🤖 Generated with Claude Code

…he inquiry

BattVoltageSensor read bytes 3-4 whatever its place in the packet. VBATT
(0x03) follows the low-noise accel on a Shimmer3 when that is on, and is
the last channel on a Shimmer3R, so the raw value came from another
sensor's bytes.

It was not reached anyway. interpretDataPacketFormat starts with
disableSensors(), and its VBatt case only counted the two bytes, so every
inquiry left the battery off. connect(), writeInfoMem and setSensors all
end with one, which undid the Shimmer3's InfoMem enable. On a Shimmer3R
the sensor was not created, configured or disabled at all.

- BattVoltageSensor gets a packetIndex and reads there. With no index
  yet, or one past the end of the packet, it adds nothing.
- The VBatt case sets the index and enables the sensor.
- The Shimmer3R branches of createSensors, initializeSensors and
  disableSensors handle it as the Shimmer3 ones do.
- The calibration moves into BattVoltageSensor.calibrate(raw:),
  unchanged: U12 at 3 V times 2, as the Java driver and the C# API use
  on both generations.

SensorTest decodes four Shimmer3R packets with the battery as the last
channel. The Shimmer3Protocol wiring is not reachable from a unit test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@marknolan
marknolan marked this pull request as ready for review November 11, 2026 16:42
@marknolan marknolan self-assigned this Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant